package gdut.jmxywz.action;

import gdut.jmxywz.been.Vertical;
import gdut.jmxywz.been.VerticalFile;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;



@SuppressWarnings("serial")
public class VerticalFilesAction extends BaseAction {
	
	private String  verId;
	private String  verticalFileId;
	private String  verticalFileName;
	private String  verticalFileContext;
	private String  verticalFileUrl;
	private List<VerticalFile>  verticalFile;
	private VerticalFile verticalFile2;
	

	/***
	 * Delete 删除附件
	 */
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String  Delete() throws Exception{
		
 
        int id = Integer.parseInt(verId);
        int filesId=0;
     
//        boolean b = this.getCommonService().deleteAllFile(id,filesId);
         boolean b = false;
         
         String hql = "from VerticalFile where verId="+id;
         System.out.println("=+++++++ "+verticalFileId);
         if(!"".equals(verticalFileId)&&verticalFileId!=null){
         	filesId = Integer.parseInt(verticalFileId);
         	
         	hql = hql + " and verticalFileId = "+filesId;
         }
         String []parem = null;
         
         System.out.println("-------hql------"+hql);
         List i = this.getCommonService().findListByHql(hql, parem);
         
         Iterator iter = i.iterator();
         
         while(iter.hasNext()){
        	 
        	 VerticalFile verticalfiles = (VerticalFile)iter.next();
        	 
        	 System.out.println("++准备删除文件++");
        	 b=this.getCommonService().deleteObject(verticalfiles);
        	 
        
        	 System.out.println("-------------删除成功--------------");
        	 
	         String path =ServletActionContext.getRequest().getRealPath("/");
	        
	         System.out.println("----------------"+path);
	         
	         path =path+verticalfiles.getVerticalFileUrl();
	         
	         System.out.println("++++"+path);
	         
	         boolean b1 = this.deleteFile(path);//输入路径，调用deletefile（）将文件从服务器中删除
	         if(b1){
	        		System.out.println("---从服务器上文件删除成功！");
	        		
	        	}else{
	        		System.out.println("----从服务器上文件删除失败！");
	        		
	        	}   	 
        	 
        	 
         }
         
         
		if(b){
			return "VerticalFilesDelete";
		}
		else{
			return "error";
		}
         
	}
	
	/***
	 * preUpdate 显示将要删除附件的信息
	 */
	
	public  String PreUpdate() throws Exception{
		 
			int  id  = 0 ;
			
			if(!"".equals(verticalFileId)&&verticalFileId!=null){
				
				id = Integer.parseInt(verticalFileId);
			}
			
			verticalFile2 =(VerticalFile) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.VerticalFile");
			
//			System.out.println("++++++++++++++++++++++++++++++++++");
//			
//			System.out.println("========="+bookmakingFile2.getBookmakingFileName());
//			
//			Bookmaking bookmaking = new Bookmaking();
//			bookmaking =bookmakingFile2.getBookmaking();
//			
//			System.out.println("-------rrr--------"+bookmaking.getBookmakingTitle());
//			
		
		return "VerticalFilesPreUpdate";
	}
	
	
	
	
	
	/***
	 * 
	 * update 修改部件
	 */
	
	
	public String  Update() throws Exception{
		
		int  fileid  = 0 ;
		int  infId = 0; 
		
		VerticalFile verticalFiles = new VerticalFile();
		
		if(!"".equals(verticalFileId)&&verticalFileId!=null&&!"".equals(verId)&&verId!=null){
			
			fileid = Integer.parseInt(verticalFileId);
			
			infId = Integer.parseInt(verId);
			
			Vertical vertical  = (Vertical)this.getCommonService().findByIntId(infId, "gdut.jmxywz.been.Vertical");
			
			verticalFiles.setVerticalFileId(fileid);
			verticalFiles.setVerticalFileContext(verticalFileContext);
			verticalFiles.setVerticalFileName(verticalFileName);
			verticalFiles.setVerticalFileUrl(verticalFileUrl);
		    
			verticalFiles.setVertical(vertical);
		}
		
	    boolean b= this.getCommonService().updateObject(verticalFiles);
		
	    if(b){
	    	
	    	System.out.println("附件信息修改成功！");
	    	
	    	return "VerticalFilesUpdate";
	    }else{
	    	
	    	System.out.println("附件信息修改失败！");
	    	
	    	return "error";
	    }
		
	}

//	
//	public  boolean DeleteFiles(String sPath) {   
//		boolean  flag = false;   
//		File   file = new File(sPath);   
//	    // 路径为文件且不为空则进行删除   
//	    if (file.isFile() && file.exists()) {   
//	        file.delete();   
//	        System.out.println("---成功从服务器删除文件---");
//	        flag = true;   
//	    }   
//	    return flag;   
//	}  
//	
	
	
	
	   /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}

	public String getVerId() {
		return verId;
	}

	public void setVerId(String verId) {
		this.verId = verId;
	}

	public String getVerticalFileId() {
		return verticalFileId;
	}

	public void setVerticalFileId(String verticalFileId) {
		this.verticalFileId = verticalFileId;
	}

	public String getVerticalFileName() {
		return verticalFileName;
	}

	public void setVerticalFileName(String verticalFileName) {
		this.verticalFileName = verticalFileName;
	}

	public String getVerticalFileContext() {
		return verticalFileContext;
	}

	public void setVerticalFileContext(String verticalFileContext) {
		this.verticalFileContext = verticalFileContext;
	}

	public String getVerticalFileUrl() {
		return verticalFileUrl;
	}

	public void setVerticalFileUrl(String verticalFileUrl) {
		this.verticalFileUrl = verticalFileUrl;
	}

	public List<VerticalFile> getVerticalFile() {
		return verticalFile;
	}

	public void setVerticalFile(List<VerticalFile> verticalFile) {
		this.verticalFile = verticalFile;
	}

	public VerticalFile getVerticalFile2() {
		return verticalFile2;
	}

	public void setVerticalFile2(VerticalFile verticalFile2) {
		this.verticalFile2 = verticalFile2;
	}



	
		
		
		
}
